package com.littlerpc.proxy.proxy;

import com.littlerpc.protocol.net.common.RpcClient;
import com.littlerpc.protocol.net.common.RpcRequest;
import lombok.Setter;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.*;

/**
 * Created by ChengPeng on 2017/6/13.
 * 这个类目前没有用到，原因是，无法使用在spring中使用注解获取代理对象
 * 如果后续有有需要，可能会在是使用这个，只是先留一个备份
 */
@Deprecated
public class ClientProxyFactoryBean implements FactoryBean<Object> {

    @Setter
    private RemoteClientInfo remoteClientInfo;
    @Setter
    private Class<?> interfaceClass;

    @Override
    public Object getObject() throws Exception {
        System.out.println("************************getObject()getObject()********************************");
        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                RpcClient client = RpcClientPoolUtil.rpcClient(remoteClientInfo);
                //构建请求对象
                RpcRequest request = RpcRequest.buildRequest(interfaceClass, method, args);
                //将请求对象通过RpcClient发送到服务提供端
                Object send = client.send(request);
                return send;
            }
        });
    }

    @Override
    public Class<?> getObjectType() {
        System.out.println("++++++++++++++" + this + "++++++++++++++++++++");
        return interfaceClass;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }

}
